package com.lenovo.leos.filebrowser.netdisk;

import java.util.ArrayList;
import java.util.List;

import com.lenovo.leos.filebrowser.R;
import com.lenovo.leos.filebrowser.ui.FileInterface;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

/** @author Steven Osborn - http://steven.bitsetters.com */
public class LenovoAdapter extends BaseAdapter implements
		OnCheckedChangeListener {

	/** Remember our context so we can use it when constructing views. */
	private Context mContext;
	private LayoutInflater mInflater;

	private List<NetFileItem> mItems = null;
	private static final String TAG = "LenovoAdapter";
	boolean flag = false;
	int checkedPos;
	boolean mChecked = false;

	public LenovoAdapter(Context context, List<NetFileItem> mItems) {
		mContext = context;
		mInflater = LayoutInflater.from(context);
		this.mItems = new ArrayList<NetFileItem>();

		if (mItems != null)
			this.mItems.addAll(mItems);
	}

	public void setListItems(List<NetFileItem> lit) {
		mItems.clear();

		if (lit != null)
			mItems.addAll(lit);
	}

	/** @return The number of items in the */
	public int getCount() {
		return mItems.size();
	}

	public Object getItem(int position) {
		return mItems.get(position);
	}

	public boolean areAllItemsSelectable() {
		return false;
	}

	/** Use the array index as a unique id. */
	public long getItemId(int position) {
		return position;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// mChecked = isChecked;
		ViewHolder holder = (ViewHolder) buttonView.getTag();
		if (isChecked
				&& !LenovoActivity.MarkedFile.contains(mItems.get(
						holder.position).getFile())) {
			LenovoActivity.MarkedFile
					.add(mItems.get(holder.position).getFile());
			LenovoActivity.isSelected = true;
		} else if (!isChecked
				&& LenovoActivity.MarkedFile.contains(mItems.get(
						holder.position).getFile())) {
			LenovoActivity.MarkedFile.remove(mItems.get(holder.position)
					.getFile());
			if (LenovoActivity.MarkedFile.isEmpty())
				LenovoActivity.isSelected = false;
		}
		if (LenovoActivity.MarkedFile.size() > 0
				&& LenovoActivity.MarkedFile.size() < LenovoAdapter.this
						.getCount()) {
			LenovoActivity.btnCancleAll.setEnabled(true);
			LenovoActivity.btnSelectAll.setEnabled(true);
			if (LenovoActivity.downFlag)
				LenovoActivity.downloadBtn.setEnabled(true);
		} else if (LenovoActivity.MarkedFile.size() < 0) {
			LenovoActivity.btnCancleAll.setEnabled(false);
			LenovoActivity.btnSelectAll.setEnabled(false);
			if (LenovoActivity.downFlag)
				LenovoActivity.downloadBtn.setEnabled(false);
		} else if (LenovoActivity.MarkedFile.size() == 0) {
			LenovoActivity.btnCancleAll.setEnabled(false);
			LenovoActivity.btnSelectAll.setEnabled(true);
			if (LenovoActivity.downFlag)
				LenovoActivity.downloadBtn.setEnabled(false);
		} else if (LenovoActivity.MarkedFile.size() == LenovoAdapter.this
				.getCount()) {
			LenovoActivity.btnCancleAll.setEnabled(true);
			LenovoActivity.btnSelectAll.setEnabled(false);
			if (LenovoActivity.downFlag)
				LenovoActivity.downloadBtn.setEnabled(true);
		}
	}

	/**
	 * @param convertView
	 *            The old view to overwrite, if one is passed
	 * @returns a IconifiedTextView that holds wraps around an NetFileItem
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		final int pos = position;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.listmainitem, null);
			holder = new ViewHolder();
			holder.checkbox = (CheckBox) convertView
					.findViewById(R.id.checkboxLayout);
			holder.checkbox.setFocusable(false);
			holder.checkbox.setOnCheckedChangeListener(this);// new
																// checkedChanged(position));
			holder.checkbox.setTag(holder);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.text = (TextView) convertView.findViewById(R.id.text);
			holder.position = pos;
			if (LenovoActivity.State == LenovoActivity._SELECT) {
				if (LenovoActivity.MarkedFile.contains(mItems.get(pos)
						.getFile())) {
					holder.checkbox.setChecked(true);
				} else {
					holder.checkbox.setChecked(false);
				}
			}
			convertView.setTag(holder);

		} else {
			// Log.i(TAG, "&&&&&&&&&&&&&&&&&&&& position = "+position);
			holder = (ViewHolder) convertView.getTag();
			holder.position = pos;
			holder.checkbox.setTag(holder);
			if (LenovoActivity.State == LenovoActivity._SELECT) {
				// Log.i(TAG, "&&&&&&&&&&&&&&&&&&&&&&&&& MarkedFile");
				if (LenovoActivity.MarkedFile.contains(mItems.get(pos)
						.getFile())) {
					holder.checkbox.setChecked(true);
				} else {
					holder.checkbox.setChecked(false);
				}
			}
		}

		if (LenovoActivity.State == LenovoActivity._SELECT) {
			holder.checkbox.setVisibility(View.VISIBLE);
		} else {
			holder.checkbox.setVisibility(View.GONE);
		}

		String s = mItems.get(pos).getText();
		if (s.startsWith("/")) {
			s = s.replaceFirst("/", "");
		}

		holder.text.setText(s);
		holder.icon.setAdjustViewBounds(true);
		holder.icon.setImageDrawable(mItems.get(pos).getIcon());

		return convertView;
	}

	public class ViewHolder {
		CheckBox checkbox;
		ImageView icon;
		TextView text;
		int position;
	}
}
